package com.suke.czx.modules.income.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.IncomeSaveGroup;
import com.suke.czx.common.validate.IncomeUpdateGroup;
import com.suke.czx.modules.income.entity.Income;
import com.suke.czx.modules.income.entity.IncomeSearchBo;
import com.suke.czx.modules.income.service.IncomeService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 收支
 * 代码描述: 收支控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/09 10:51:20
 */
@Validated
@RestController
@RequestMapping("/api/income")
@AllArgsConstructor
@Api(value = "IncomeController", tags = "收支管理")
public class IncomeController extends AbstractController {

    private final IncomeService incomeService;

    /**
     * 分页查询收支列表
     * @param incomeSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:income:list')")
    public R list(IncomeSearchBo incomeSearchBo) {
        if (incomeSearchBo.getPage() == null || incomeSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询收支列表
        return incomeService.pageIncomeList(incomeSearchBo);
    }

    /**
     * 保存收支
     * @param income 收支
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存收支")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:income:save')")
    public R save(@Validated(IncomeSaveGroup.class) Income income, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        income.setCreator(getUserId());
        income.setCreateTime(new Date());
        incomeService.save(income);
        return R.ok();
    }

    /**
     * 更新收支
     * @param income 收支
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新收支")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:income:update')")
    public R update(@Validated(IncomeUpdateGroup.class) Income income, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        income.setUpdater(getUserId());
        income.setUpdateTime(new Date());
        incomeService.updateById(income);
        return R.ok();
    }

    /**
     * 删除收支
     * @param incomeId 收支编码
     * @return VO
     */
    @SysLog("删除收支")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:income:delete')")
    public R delete(@NotNull(message = "收支编码不能为空") Long incomeId) {
        return incomeService.updateDelIncome(incomeId, getUserId());
    }

}